local skel = fk.CreateSkill {
  name = "lb__huli",
}

Fk:loadTranslationTable {
  ["lb__huli"] = "互礼",
  --[":lb__huli"] = "每回合各限一次，当你使用牌指定唯一目标后，或当你成为牌的目标后，你可以与对方依次将一张手牌置于牌堆同一端，然后双方依次从另一端摸一张牌。",
  --10.3
  --[":lb__huli"] = "当你横置或重置后，若你有手牌，你可以观看当前回合角色的手牌，将你与其各一张手牌置于牌堆同一端，然后与其依次从另一端摸一张牌。",
  --10.25
  [":lb__huli"] = "当你不因此横置或重置后，若你有手牌，你可以观看一名角色的手牌，将你与其各一张手牌置于牌堆同一端，然后你与其依次从另一端摸一张牌并复原武将牌。",


  --["#lb__huli"] = "互礼：你可以与 %dest 依次将一张手牌置于牌堆同一端",
  --["#lb__huli_put"] = "互礼：请将一张手牌置于%arg",
  --["#lb__huli"] = "互礼：你可以观看 %dest 的手牌，将你与其各一张手牌置于牌堆同一端",
  ["#lb__huli"] = "互礼：你可以观看一名角色的手牌并与其依次复原武将牌，将你与其各一张手牌置于牌堆同一端并依次从另一端摸一张牌",
  ["#lb__huli_choose"] = "互礼：将你与 %dest 各一张手牌置于牌堆顶或牌堆底",
  ["#lb__huli_put"] = "互礼：请将 %arg 张手牌置于牌堆顶或牌堆底",
  --["#lb__huli_put"] = "互礼：请将一张手牌置于%arg",

  ["$lb__huli1"] = "盛宴在即，想来共演一曲吗？",
  ["$lb__huli2"] = "眼下不失为助兴的好时机。",
}

Fk:addPoxiMethod {
  name = skel.name,
  card_filter = function(to_select, selected, data, extra_data)
    local ids = {}
    for _, flag in ipairs(data) do
      if table.find(flag[2], function(id)
            return table.contains(selected, id)
          end) then
        table.insertTable(ids, flag[2])
      end
    end
    return #selected < 2 and not table.contains(ids, to_select)
  end,
  feasible = function(selected, data, extra_data)
    return #selected == 2
  end,
  default_choice = function(data, extra_data)
    return { data[1][2][1], data[2][2][1] }
  end,
  post_select = function(selected, data, extra_data)
    local ids = {} --排序
    for _, flag in ipairs(data) do
      for _, id in ipairs(flag[2]) do
        if table.contains(selected, id) then
          table.insert(ids, id)
        end
      end
    end
    return ids
  end,
  prompt = function(data, extra_data)
    return extra_data.prompt
  end,
}

skel.extra.choiceFilter = function(cards, choice, extra_data)
  return #cards == 1
end
--[[
local huli_spec = {
  max_turn_use_time = 1,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    local to = event == fk.TargetSpecified and data.to or data.from
    if player:hasSkill(skel.name) and target == player and to ~= player
        and not player:isKongcheng() and not data.to:isKongcheng() then
      if event == fk.TargetSpecified then
        return data:isOnlyTarget(to)
      end
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = event == fk.TargetSpecified and data.to or data.from
    local cards, choice = room:askToChooseCardsAndChoice(player, {
      cards = player:getCardIds("h"),
      skill_name = skel.name,
      prompt = "#lb__huli::" .. to.id,
      choices = { "Top", "Bottom" },
      cancel_choices = { "Cancel" },
      min_num = 1,
      max_num = 1,
      filter_skel_name = skel.name,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, { cards = cards, choice = choice })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = event == fk.TargetSpecified and data.to or data.from
    local room = player.room
    local cost_data = event:getCostData(self)
    local choice = cost_data.choice
    local cards = cost_data.cards
    local list = { "top", "bottom" }
    local put = table.remove(list, table.indexOf(list, (choice == "Top" and "top" or "bottom")))
    local draw = list[1]
    local putCard = function(proposer, ids)
      local move = {
        ids = table.reverse(ids),
        from = proposer,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        --drawPilePosition = -1,
        skillName = skel.name,
        proposer = proposer,
        moveVisible = true,
      }
      if put == "bottom" then
        move.ids = ids
        move.drawPilePosition = -1
      end
      room:moveCards(move)
    end
    if #cards > 0 then
      putCard(player, cards)
      local ids = to:getCardIds("h")
      if #ids > 1 then
        ids = room:askToCards(to, {
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          cancelable = false,
          include_equip = false,
          prompt = "#lb__huli_put:::" .. Fk:translate(choice),
        })
      end
      if #ids > 0 then
        putCard(to, ids)
        player:drawCards(1, skel.name, draw)
        to:drawCards(1, skel.name, draw)
      end
    end
  end,
}

skel:addEffect(fk.TargetSpecified, huli_spec, { check_effect_limit = true })

skel:addEffect(fk.TargetConfirmed, huli_spec, { check_effect_limit = true })
--]]

skel:addEffect(fk.ChainStateChanged, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    --local current = player.room:getCurrent()
    return --current and
        player:hasSkill(skel.name) and target == player and
        not (data.reason and data.reason == skel.name) and
        not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room:getAlivePlayers(), function(to)
      return not to:isKongcheng()
    end)
    if #tos == 0 then return end
    tos = room:askToChoosePlayers(player, {
      targets = tos,
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      cancelable = true,
      prompt = "#lb__huli",
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
    --return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__huli::" .. player.room:getCurrent().id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local list = { "top", "bottom" }
    local to = event:getCostData(self).tos[1] --room:getCurrent()
    local putCard = function(proposer, ids, put)
      local move = {
        ids = table.reverse(ids),
        from = proposer,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        --drawPilePosition = -1,
        skillName = skel.name,
        proposer = proposer,
        moveVisible = false,
      }
      if put == "bottom" then
        move.ids = ids
        move.drawPilePosition = -1
      end
      room:moveCards(move)
    end
    if player == to then
      local num = math.min(2, #player:getCardIds("h"))
      local cards, choice = room:askToChooseCardsAndChoice(player, {
        cards = player:getCardIds("h"),
        skill_name = skel.name,
        prompt = "#lb__huli_put:::" .. num,
        choices = { "Top", "Bottom" },
        --cancel_choices = { "Top" },
        min_num = num,
        max_num = num,
        --filter_skel_name = skel.name,
      })
      if #cards > 0 then
        local put = table.remove(list, table.indexOf(list, (choice == "Top" and "top" or "bottom")))
        local draw = list[1]
        putCard(player, cards, put)
        if player:isAlive() then
          player:drawCards(#cards, skel.name, draw)
          player:reset()
        end
      end
    else
      local cards_1 = player:getCardIds("h")
      local cards_2 = to:getCardIds("h")
      local show = room:askToPoxi(player, {
        poxi_type = skel.name,
        cancelable = false,
        data = {
          { player:__touistring(), cards_1 },
          { to:__touistring(),     cards_2 },
        },
        extra_data = {
          prompt = "#lb__huli_choose::" .. to.id,
        },
      })
      if #show > 0 then
        local put = room:askToViewCardsAndChoice(player, {
          cards = show,
          skill_name = skel.name,
          choices = { "Top", "Bottom" },
          prompt = "#lb__huli_choose::" .. to.id,
        })
        put = table.remove(list, table.indexOf(list, (put == "Top" and "top" or "bottom")))
        local draw = list[1]
        for i, id in ipairs(show) do
          local proposer = i == 1 and player or to
          if table.contains(proposer:getCardIds("h"), id) then
            putCard(proposer, { id }, put)
            proposer:drawCards(1, skel.name, draw)
            proposer:reset()
          end
        end
      end
    end
  end,
})

return skel
